package edu.jkd.controller;


import edu.jkd.utils.QiniuUtils;
import lombok.val;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/qiniutest")
public class PictureController {

    @RequestMapping("/upload")
    @ResponseBody
    public Map upload(MultipartFile myfile) throws IOException { // 文件接收对象
        Map map = new HashMap<>();
        if (myfile != null){
            System.out.printf(myfile.getBytes().toString());
            try {
                QiniuUtils qiniuUtils = new QiniuUtils();
                String imgUrl = qiniuUtils.upload(myfile.getBytes(),myfile.getOriginalFilename());
                map.put("imgUrl", imgUrl);
            } catch (IOException e) {
                e.printStackTrace();
                map.put("imgUrl", null);
            }
        }
        return map;
    }
}